package com.sgb.kjwl.view.ui.activity.pick;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Gravity;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPhotoDownloadViewBinding;
import com.swgk.core.download.DownloadListner;
import com.swgk.core.download.DownloadManager;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.ThreadUtils;

import java.io.File;

public class PhotoDownloadViewActivity extends AppActivity {
    ActivityPhotoDownloadViewBinding binding;
    private String url;

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        url = getIntent().getStringExtra("url");
        String title = getIntent().getStringExtra("title");

        binding = DataBindingUtil.setContentView(PhotoDownloadViewActivity.this, R.layout.activity_photo_download_view);
        binding.titleView.setText(title);
        binding.ivBack.setOnClickListener(this);
        binding.imageDownload.setOnClickListener(this);
        binding.rel.setOnClickListener(this);

        binding.photoView.enable();
        RequestOptions options = new RequestOptions()
                .fitCenter();
        Glide.with(this)
                .load(url)
                .apply(options)
                .into(binding.photoView);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
            case R.id.rel:
                finish();
                break;
            case R.id.image_download:
                DownloadImageData(url);
                break;
        }
    }

    //下载图片
    private void DownloadImageData(String url) {
        //执行下载图片方法
        DownloadManager mDownloadManager = DownloadManager.getInstance();
        String fileName = mDownloadManager.getFileName(url);
        String galleryPath = Environment.getExternalStorageDirectory()
                + File.separator + Environment.DIRECTORY_DCIM
                + File.separator + "Camera" + File.separator;
        mDownloadManager.add(url, galleryPath, fileName, new DownloadListner() {
            @Override
            public void onFinished() { //下载完成
                onFinishedData(galleryPath, fileName);
            }

            @Override
            public void onProgress(float progress, boolean isDownloading) { //更新中
                MLog.e("自动更新", "更新中" + progress);
            }

            @Override
            public void onPause() { //暂停
                MLog.e("自动更新", "暂停了");
            }

            @Override
            public void onCancel() { //取消
                MLog.e("自动更新", "下载已取消");
            }
        });
        mDownloadManager.download(url); //开始下载
    }

    private void onFinishedData(String galleryPath, String fileName) {
        ThreadUtils.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                binding.imageDownload.setVisibility(View.GONE);
                MToast.showLongToast1(getActivity(), "图片保存成功！", Gravity.CENTER);
                getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + galleryPath + "/" + fileName)));
            }
        });
    }
}
